package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Scanner;

import model.Field;
import model.OthelloBoard;
import model.Position;


@SuppressWarnings("serial")
public class BoardPanel extends GridBagWindow implements ActionListener
{
	OthelloBoard d_board;
    
    static final Color  s_emptytile = Color.GRAY;
    static final Color  s_blacktile = Color.BLACK;
    static final Color  s_whitetile = Color.WHITE;
    static final Color[] s_tiles = new Color[3];
    
    static final int    s_tilesize = 70;
    
    public boolean input_set = false;
    
    Position      d_clickedposition;
    boolean       d_clicked;
    PositionButton [] [] d_buttons;
    
    public BoardPanel(OthelloBoard board)
    {
    	d_board = board;
    	
    	s_tiles[Field.BLACK] = s_blacktile;
    	s_tiles[Field.WHITE] = s_whitetile;
    	s_tiles[Field.EMPTY] = s_emptytile;
    	
        d_clicked         = true;
        d_clickedposition = null;
        
        createBoard();
    }
    
    private void createBoard()
    {
        d_buttons = new PositionButton [8] [8];
        for(int indexx = 0; indexx < d_buttons.length; ++indexx)
            for(int indexy = 0; indexy < d_buttons.length; ++indexy)
            {
                d_buttons[indexx][indexy] = createButton("", indexx + " " + indexy, "", s_tiles[Field.EMPTY], s_tilesize, this);
                addComponentToPane(d_buttons[indexx][indexy], indexx, indexy, 1);
            }
    }
    
    public void updateBoard(byte color)
    {
    	/* get board ready for player with color*/
    	
    	//reset info on board
        for(int indexx = 0; indexx < d_buttons.length; ++indexx)
            for(int indexy = 0; indexy < d_buttons.length; ++indexy)
            {   
                byte color_b = d_board.at(indexx, indexy);
                PositionButton button = d_buttons[indexx][indexy]; 
                button.setBackground(s_tiles[color_b]);
                button.setLegalMove(null);
                button.setChosenMove(null);
            }
        
        //set info about transitions
        ArrayList<Position> legalmoves = d_board.getTransitions(color);
        for(int index = 0; index < legalmoves.size(); ++index)
        {
            int indexx = legalmoves.get(index).x;
            int indexy = legalmoves.get(index).y;
            PositionButton  button = d_buttons[indexx][indexy]; 
            button.setLegalMove(s_tiles[color]);
            
        }

        //set info about last move
        if(d_board.last_move != null)
        {
        	PositionButton button = d_buttons[d_board.last_move.x][d_board.last_move.y];
        	button.setChosenMove(s_tiles[Field.EMPTY]);
        }
    }

    public static PositionButton createButton(String title, String action, String tooltiptext, Color color, int size, ActionListener actionlistener)
    {
        PositionButton button = new PositionButton(title);

        button.setBackground(color);
        button.setActionCommand(action);
        button.addActionListener(actionlistener);
        button.setToolTipText(tooltiptext);
        button.setPreferredSize(new Dimension(size,size));
        button.setOpaque(true);
        return button;
    }


    public void actionPerformed(ActionEvent action)
    {
        String event = action.getActionCommand();
        Scanner scanner = new Scanner(event);
        clicked(scanner.nextInt(),scanner.nextInt());
    }
    
    private void clicked(int indexx, int indexy)
    {
        if (d_clicked == false)
        {
            d_clicked         = true;
            d_clickedposition = new Position((short)indexx, (short)indexy);
            System.out.println(indexx + " " + indexy);
        }
    }

    public Position getMove(ArrayList<Position> possiblemoves)
    {
        d_clicked         = false;
        while(!input_set)
        {
            if(d_clicked == true)
            {
                if(checkLegalMove(d_clickedposition,possiblemoves))
                    return d_clickedposition;
                else
                {
                    d_clickedposition = null;
                    d_clicked         = false;
                }      
            }  
            try
            {
                Thread.sleep(200);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        return null;
    }

    private boolean checkLegalMove(Position move, ArrayList<Position> possiblemoves)
    {
        for(Position possiblemove : possiblemoves) 
            if(possiblemove.x == move.x && possiblemove.y == move.y) 
                return true;  
        return false;
    }

	public void updateFinalBoard() 
	{
		int whiteStones = d_board.getScore(Field.WHITE);
		int blackStones = d_board.getScore(Field.BLACK);
		//reset info on board
        for(int indexx = 0; indexx < d_buttons.length; ++indexx)
            for(int indexy = 0; indexy < d_buttons.length; ++indexy)
            {   
                PositionButton button = d_buttons[indexx][indexy]; 
                button.setBackground(s_tiles[(indexy * 8 + indexx < whiteStones) ?               Field.WHITE : 
                			                 (indexy * 8 + indexx < whiteStones + blackStones) ? Field.BLACK : Field.EMPTY]);
                button.setLegalMove(null);
                button.setChosenMove(null);
            }
	}
}
